﻿/* DES加密、解密类
 */

/*待测试*/
namespace LSLib.Security
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class DESEncryptHelper
    {
        /// <summary>生成DES密钥
        /// </summary>
        public static string GenerateKey()
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
        }

        /// <summary>DES加密类
        /// </summary>
        public class EncryptCls
        {
            #region 定义属性
            /// <summary>待加密内容
            /// </summary>
            public string Input { get; set; }

            /// <summary>DES加密密钥
            /// </summary>
            public string DesKey { get; set; }
            #endregion

            public EncryptCls(string input, out string desKey)
            {
                this.Input = input;
                desKey = GenerateKey();
                this.DesKey = desKey;
            }

            /// <summary>DES加密算法
            /// </summary>
            public string DESEncrypt()
            {
                DESCryptoServiceProvider MyDes_E = new DESCryptoServiceProvider();
                MemoryStream MyMem_E = new MemoryStream();
                CryptoStream MyCry_E = null;
                try
                {
                    byte[] MyStr_E = Encoding.UTF8.GetBytes(this.Input);
                    byte[] MyKey_E = Encoding.UTF8.GetBytes(this.DesKey);

                    MyDes_E.Key = MyKey_E;
                    MyDes_E.IV = MyKey_E;

                    MyCry_E = new CryptoStream(MyMem_E, MyDes_E.CreateEncryptor(), CryptoStreamMode.Write);
                    MyCry_E.Write(MyStr_E, 0, MyStr_E.Length);
                    MyCry_E.FlushFinalBlock();

                    return Convert.ToBase64String(MyMem_E.ToArray());
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("DES加密失败：{0}!", ex.Message));
                }
                finally
                {
                    MyMem_E.Dispose();
                }
            }
        }

        /// <summary>DES解密类
        /// </summary>
        public class DecryptCls
        {
            #region 定义属性
            /// <summary>待解密内容
            /// </summary>
            public string Input { get; set; }

            /// <summary>DES解密密钥
            /// </summary>
            public string DesKey { get; set; }
            #endregion

            public DecryptCls(string input, string desKey)
            {
                this.Input = input;
                this.DesKey = desKey;
            }

            /// <summary>DES解密算法
            /// </summary>
            public string DESDecrypt()
            {
                DESCryptoServiceProvider MyDes_D = new DESCryptoServiceProvider();
                MemoryStream MyMem_D = new MemoryStream();
                CryptoStream MyCry_D = null;
                try
                {
                    byte[] MyStr_D = Convert.FromBase64String(this.Input);
                    byte[] MyKey_D = Encoding.UTF8.GetBytes(this.DesKey);

                    MyDes_D.Key = MyKey_D;
                    MyDes_D.IV = MyKey_D;

                    MyCry_D = new CryptoStream(MyMem_D, MyDes_D.CreateDecryptor(), CryptoStreamMode.Write);
                    MyCry_D.Write(MyStr_D, 0, MyStr_D.Length);
                    MyCry_D.FlushFinalBlock();

                    return Encoding.UTF8.GetString(MyMem_D.ToArray());
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("DES解密失败：{0}!", ex.Message));
                }
                finally
                {
                    MyMem_D.Dispose();
                }
            }
        }
    }
}